package main

import (
	"encoding/json"
	"fmt"
	"helloworld/model"
)

type Person struct {
	Name string
	Age  int
}

type Student struct {
	Name string
	Age  int
}

type Monster struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	Skill string `json:"skill"`
}

type A struct {
	Num int
}

func (a A) test() {
	fmt.Println(a.Num)
}

type Circle struct {
	radius float64
}

func (c Circle) area() float64 {
	return 3.14 * c.radius * c.radius
}

func (c Circle) area2() float64 {
	c.radius = 3
	fmt.Printf("c的地址：%p\n", &c)
	return 3.14 * c.radius * c.radius
}

func (c *Circle) area3() float64 {
	c.radius = 3 // (*c).radius = 3 的简写
	fmt.Printf("c的地址：%p\n", c)
	return 3.14 * c.radius * c.radius
}

type Student1 struct {
	Name   string
	Gender string
	Age    int
	Id     int
	Score  float64
}

func (stu *Student1) say() string {
	return fmt.Sprintf("Name:%v; Gander=%v; Age=%v;Id=%v; Score=%v", stu.Name, stu.Gender, stu.Age, stu.Id, stu.Score)
}

func main04() {
	//testDef()
	//test2()
	//test3()
	a := A{12}
	a.test()

	c1 := Circle{2}
	c1.area3() // (&c1).area3()
	fmt.Printf("c1的地址：%p\n", &c1)
	fmt.Println(c1.radius)

	//注意这种用法，可以很直观的看到给哪个变量赋值，且可以省略某些变量的赋值，而下面的方式只能全部赋值
	stu := Student1{Name: "张三", Gender: "男", Age: 12, Id: 1}
	stu1 := Student1{"张三", "男", 12, 1, 99.9}
	fmt.Println(stu.say())
	fmt.Println(stu1.say())

	student2 := model.Student2{"张三", 12}
	fmt.Println(student2)

	student3 := model.NewStudent3("张三", 12)
	fmt.Println(student3.Name, student3.Age)

}

func test3() {
	p := Person{"zhangsan", 12}
	var s Student = Student(p)
	fmt.Println(p)
	fmt.Println(s)

	type Stu Student
	var stu Stu = Stu(s)
	fmt.Println(stu)

	monster := Monster{"牛魔王", 500, "芭蕉扇"}
	jsonByteArr, err := json.Marshal(monster)
	if err != nil {
		fmt.Println("序列化json异常", err)
	} else {
		fmt.Println("json字符串:", string(jsonByteArr))
	}
}

func test2() {
	p := Person{"张三", 12}
	var p1 Person = p
	fmt.Printf("p的值:%v p的地址:%p\n", p, &p)
	fmt.Printf("p1的值:%v p1的地址：%p\n", p1, &p1)

	var p3 *Person = &p
	p3.Name = "李四"
	fmt.Printf("p的值:%v p的地址:%p\n", p, &p)
	fmt.Printf("p3的值:%p p3的地址：%p\n", p3, &p3)
}

func testDef() {
	var person Person
	person.Name = "张三"
	person.Age = 12
	fmt.Println(person)

	//var person2 Person = Person{"张三", 12}
	var person2 = Person{"张三", 12}
	fmt.Println(person2)
	person3 := Person{"张三", 12}
	fmt.Println(person3)

	//var person4 *Person = new(Person)
	var person4 = new(Person)
	(*person4).Name = "张三"
	person4.Age = 12
	fmt.Println(*person4)

	var person5 *Person = &Person{}
	//var person5 = &Person{}
	(*person5).Name = "张三"
	person5.Age = 12
	fmt.Println(person5)
	var person6 *Person = &Person{"张三", 12}
	//person6 := &Person{"张三", 12}
	fmt.Println(person6)
}
